import { getToken } from '@/utils/auth';
import * as weixin from '@/utils/weixin';

import { store } from '@/store';
import Utils from '@/utils/utils';

let isAndroid = Utils.browser().android;
export default (router) => {
  router.beforeEach(async (to, from, next) => {
    if (!isAndroid) {
      weixin.init();
    }
    // 监听床位
    if (store.state.global.settings.isDisableBed) {
      if (to.name === 'Bed') {
        next();
      } else {
        next({ name: 'Bed', replace: true });
      }
      return;
    }

    // 判断是否登录
    if (getToken()) {
      next();
      return;
    }

    next();
  });

  router.afterEach(() => {
    // 微信sdk
    if (isAndroid) {
      weixin.init();
    }
  });
};
